import sdljava.SDLException;
import sdljava.event.*;

public class Base extends ConstructionLocations{
	
	public Base(GameWindow gameWindow, String imageFile) throws SDLException{
		super(gameWindow, imageFile);
		populateSVector();
	}
	/*public void populateSVector(Vector<MaterialSprite> materials){
		for(int i = 0; i < materials.size(); i++){
			//populate sprite vector
		}
	}*/

	public void handleEvent(SDLEvent event) throws SDLException{
		if(input != null){
			if(input.handleEvent(event)){
				//materials.getAvailableVector().setElementAt(obj,(materials.getSiteMaterial().indexOf(userAnswer.getChosenMaterial()))
				//System.out.println(userAnswer.size());
				/*for(int i = 0; i < userAnswer.size(); i++){
					System.out.println(userAnswer.get(i).getChosenMaterial().getType());
					System.out.println(userAnswer.get(i).getAmount());
				}*/
				input = null;
				show();
			}
		}
		else{ 
			if(event.getType() == SDLEvent.SDL_MOUSEMOTION){
				for(int i = 0; i < buttons.size(); i++){
					buttons.get(i).spriteHandle(event);
				}
			}
			//check which material has been selected
			//check if location of cursor is inside button boundaries
			if(event.getType() == SDLEvent.SDL_MOUSEBUTTONDOWN){
				int currentX = ((SDLMouseButtonEvent)event).getX();
				int currentY = ((SDLMouseButtonEvent)event).getY();
				if(currentX >= 0 
						&& currentY >= 635 
						&& currentX <= 165 
						&& currentY <= 800){
					input = new Input(gameWindow, userAnswer, "bitmaps/enter quantity.bmp", siteResources.getAvailableMaterials().get(0));
					input.show();
				}else{
					for(int i = 0; i < buttons.size(); i++){
						if(buttons.get(i).spriteHandle(event) == true){
							if(i == 1){
								if(!userAnswer.isEmpty()){
									/*if(check()){
										changeLocation = 4;
									}else{
										changeLocation = 5;
									}*/
									changeLocation = 1;
								}
							}
							else{
								changeLocation = 2;
							}
						}	
					}
				}
			}
		}
	}
	public Boolean check(){
		Boolean check = true, checkType = false, checkArea = false;
		String selectedtype = null;
		Boolean sameType = true;
		if(!userAnswer.isEmpty()){
			selectedtype = userAnswer.get(0).getChosenMaterial().getType();
			for(int i = 0; i < userAnswer.size(); i++){
				if(userAnswer.get(i).getChosenMaterial().getType() != selectedtype){
					sameType = false;
					break;
				}
			}
			if(sameType){
				for(int i = 0; i < type.size(); i++){
					for(int j = 0; j < userAnswer.size(); j++){
						if(type.get(i) == selectedtype){
							checkType = true;
						}
					}
				}
				if(calculateUserArea().equals(calculateAnswer())){
					checkArea = true;
				}
			}
		}
		//Bugged method if adding hint system separate all of the below checks
		//into separate 'if' statements
		//Could add all checks to a Boolean vector and then for loop through the 
		//Vector searching for falses so as to print out hints.
		if(checkType == false || checkArea == false){
			check = false; System.out.println("base check failed");
		}
		return check;
	}
	public Double calculateAnswer(){
		return length*width;
	}
	public Double calculateUserArea(){
		Double answer = 0.0;
		for(int i = 0; i < userAnswer.size(); i++){
			answer += userAnswer.get(i).getAmount()*userAnswer.get(i).getChosenMaterial().getLength()*userAnswer.get(i).getChosenMaterial().getWidth();
		}
		//System.out.println("base user answer" + answer);
		return answer;
	}
	public void setAnswers(Double Length, Double Width, String Type, String Shape){
		this.length = Length;
		this.width = Width;
		this.type.add(Type);
		this.shape = Shape;
	}
}
